package com.example.factory.data.helper;


import android.text.TextUtils;

import com.example.common.factory.data.DataSource;
import com.example.factory.Factory;
import com.example.factory.R;
import com.example.factory.model.api.AccountRspModel;
import com.example.factory.model.api.RspModel;
import com.example.factory.model.api.account.LoginModel;
import com.example.factory.model.api.account.RegisterModel;
import com.example.factory.model.db.User;
import com.example.factory.net.NetWork;
import com.example.factory.net.RemoteService;
import com.example.factory.persistence.Account;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by HongQian.Wang on 2017/10/3.
 */

public class AccountHelper {
    public static void register(RegisterModel model, final DataSource.Callback<User> callback) {
        RemoteService service = NetWork.getRetrofit().create(RemoteService.class);
        Call<RspModel<AccountRspModel>> call = service.accountRegisterModel(model);
        call.enqueue(new AccountRspCallback(callback));


    }

    /**
     * 登录的调用
     *
     * @param model    登录的Model
     * @param callback 成功与失败的接口回送
     */
    public static void login(final LoginModel model, final DataSource.Callback<User> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = NetWork.remote();
        // 得到一个Call
        Call<RspModel<AccountRspModel>> call = service.accountLogin(model);
        // 异步的请求
        call.enqueue(new AccountRspCallback(callback));
    }





//对设备Id进行绑定的操作
    public static void bindPush(DataSource.Callback<User> callback) {
                String pushId=Account.getPushId();
                if(TextUtils.isEmpty(pushId)) {
                    return;
                }

        RemoteService service = NetWork.remote();
        Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
        call.enqueue(new AccountRspCallback(callback));



    }

    private  static class AccountRspCallback implements Callback<RspModel<AccountRspModel>>{
        final DataSource.Callback<User> callback;
        public AccountRspCallback(DataSource.Callback<User> callback) {
           this.callback=callback;
        }

        @Override
        public void onResponse(Call<RspModel<AccountRspModel>> call, Response<RspModel<AccountRspModel>> response) {
            RspModel<AccountRspModel> rspModel = response.body();
            if (rspModel.success()) {
                //拿到实体
                AccountRspModel accountRspModel = rspModel.getResult();
                //获取我的信息
                final User user=accountRspModel.getUser();


                DbHelper.save(User.class,user);

                //同步到 持久化中
                Account.login(accountRspModel);
                //判断是否绑定设备
                if(accountRspModel.isBind()) {
                   //设置绑定状态
                    Account.setBind(true);
                     if(callback!=null) {
                         callback.onDataLoaded(user);
                     }

                }else {
                    bindPush(callback);
                }



            }else {
//                    //对返回的RspM哦的两种的失败的code进行解析,解析对应的String资源上面
                Factory.decodeRspCode(rspModel,callback);
            }
        }

        @Override
        public void onFailure(Call<RspModel<AccountRspModel>> call, Throwable t) {
            // 网络请求失败
            if (callback != null)
                callback.onDataNotAvailable(R.string.data_network_error);
        }
        }
    }

